package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.common.LineFunction;
import com.ruoyi.syrw.common.LineUtil;
import com.ruoyi.report_tg.domain.vo.GeneralCharDataVo;
import com.ruoyi.report_wjjhl.domain.WdcledtaddbzqxBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdcledtaddbzqxBaDetailVo;
import com.ruoyi.report_wjjhl.domain.vo.WdcledtaddbzqxbgBsInfoVo;
import com.ruoyi.report_wjjhl.mapper.WdcledtaddbzqxBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdcledtaddbzqxbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.WdcledtaddbzqxbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdcledtaddbzqxbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdcledtaddbzqxbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料EDTA滴定标准曲线试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-08-05
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdcledtaddbzqxbgBsInfoServiceImpl implements IWdcledtaddbzqxbgBsInfoService {

    private final WdcledtaddbzqxbgBsInfoMapper baseMapper;
    private final WdcledtaddbzqxBaDetailMapper detailMapper;

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测报告
     */
    @Override
    @FIllBasicInfo
    public WdcledtaddbzqxbgBsInfoVo queryById(Long excelId){
        WdcledtaddbzqxbgBsInfoVo wdcledtaddbzqxbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdcledtaddbzqxBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdcledtaddbzqxBaDetail>()
            .eq(WdcledtaddbzqxBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdcledtaddbzqxbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 计算画图
        compute(wdcledtaddbzqxbgBsInfoVo);
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(wdcledtaddbzqxbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(wdcledtaddbzqxbgBsInfoVo.getJiancjl())) {
            return wdcledtaddbzqxbgBsInfoVo;
        }
        return jcjl(wdcledtaddbzqxbgBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测报告列表
     */
    @Override
    public TableDataInfo<WdcledtaddbzqxbgBsInfoVo> queryPageList(WdcledtaddbzqxbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdcledtaddbzqxbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdcledtaddbzqxbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测报告列表
     */
    @Override
    public List<WdcledtaddbzqxbgBsInfoVo> queryList(WdcledtaddbzqxbgBsInfoBo bo) {
        LambdaQueryWrapper<WdcledtaddbzqxbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdcledtaddbzqxbgBsInfo> buildQueryWrapper(WdcledtaddbzqxbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdcledtaddbzqxbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdcledtaddbzqxbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdcledtaddbzqxbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), WdcledtaddbzqxbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdcledtaddbzqxbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdcledtaddbzqxbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdcledtaddbzqxbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdcledtaddbzqxbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdcledtaddbzqxbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdcledtaddbzqxbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdcledtaddbzqxbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), WdcledtaddbzqxbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdcledtaddbzqxbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdcledtaddbzqxbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdcledtaddbzqxbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdcledtaddbzqxbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdcledtaddbzqxbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), WdcledtaddbzqxbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), WdcledtaddbzqxbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), WdcledtaddbzqxbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), WdcledtaddbzqxbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdcledtaddbzqxbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), WdcledtaddbzqxbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), WdcledtaddbzqxbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdcledtaddbzqxbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdcledtaddbzqxbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), WdcledtaddbzqxbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdcledtaddbzqxbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdcledtaddbzqxbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFangcs()), WdcledtaddbzqxbgBsInfo::getFangcs, bo.getFangcs());
        lqw.eq(bo.getCsk() != null, WdcledtaddbzqxbgBsInfo::getCsk, bo.getCsk());
        lqw.eq(bo.getCsb() != null, WdcledtaddbzqxbgBsInfo::getCsb, bo.getCsb());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料EDTA滴定标准曲线试验检测报告
     */
    @Override
    public Boolean insertByBo(WdcledtaddbzqxbgBsInfoBo bo) {
        WdcledtaddbzqxbgBsInfo add = BeanUtil.toBean(bo, WdcledtaddbzqxbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 5次
            ArrayList<WdcledtaddbzqxBaDetail> details = new ArrayList<>(5);
            for (long i = 1; i <= 5L; i++) {
                details.add(new WdcledtaddbzqxBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料EDTA滴定标准曲线试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(WdcledtaddbzqxbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        WdcledtaddbzqxbgBsInfo update = BeanUtil.toBean(bo, WdcledtaddbzqxbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdcledtaddbzqxBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(WdcledtaddbzqxbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (WdcledtaddbzqxBaDetailVo detailVo : bo.getBaDetailVos()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdcledtaddbzqxbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料EDTA滴定标准曲线试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WdcledtaddbzqxbgBsInfoVo compute(WdcledtaddbzqxbgBsInfoVo vo) {
        // 1. 参数判断 如果有任何一个为空,则 直接原样返回
        List<WdcledtaddbzqxBaDetailVo> detailVos = vo.getBaDetailVos();
        if (CollUtil.isEmpty(detailVos)) {
            return vo;
        }
        // 1.2 非空判断
        for (WdcledtaddbzqxBaDetailVo detailVo : detailVos) {
            if (BigDecimalUtil.isAnyOneEmpty(detailVo.getEdtahlpjz(), detailVo.getHuijl())) {
                return vo;
            }
        }
        double[][] data = new double[detailVos.size()][2];
        for (int i = 0; i < detailVos.size(); i++) {
            data[i][0] = detailVos.get(i).getHuijl().doubleValue();
            data[i][1] = detailVos.get(i).getEdtahlpjz().doubleValue();
        }
        // 2 计算
        LineFunction generalLine = LineUtil.getGeneralLine(data);
        // 3 画图的数据
        // 3.1 画图所需要的 data  参与画线 不展示 不突出辅助线
        List<GeneralCharDataVo> charData = generateData(generalLine, detailVos.get(0).getHuijl().doubleValue(),
            detailVos.get(detailVos.size()-1).getHuijl().doubleValue());
        // 3.2 其他的点 不参与画线 展示 不突出辅助线
        for (double[] datum : data) {
            charData.add(new GeneralCharDataVo(datum, "010"));
        }
//        // 3.3 参与画线 展示 突出辅助线
//        charData.add(new GeneralCharDataVo(new double[]{vo.getZuijhsl().doubleValue(), vo.getZuidgmd().doubleValue()}, "011"));
        vo.setChartData(charData);
        // 4 函数式
        vo.setFangcs("y=" + BigDecimalUtil.defaultValue(generalLine.getK(), 3)
            + "x+" + BigDecimalUtil.defaultValue(generalLine.getB(), 3));
        return vo;
    }

    private List<GeneralCharDataVo> generateData(LineFunction generalLine, double start, double end) {
        List<GeneralCharDataVo> result = new ArrayList<>(20);
        result.add(new GeneralCharDataVo(new double[]{BigDecimalUtil.defaultValue(start,2).doubleValue(), generalLine.getY(start)}, "100"));
        result.add(new GeneralCharDataVo(new double[]{BigDecimalUtil.defaultValue(end,2).doubleValue(), generalLine.getY(end)}, "100"));
        return result;
    }

    /**
     * 生成 无机结合料稳定材料EDTA滴定标准曲线试验检测报告 检测结论
     */
    @Override
    public WdcledtaddbzqxbgBsInfoVo jcjl(WdcledtaddbzqxbgBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，符合《水泥稳定碎石基层施工技术规范》DB37/T 3577-2019及设计文件要求。");
        return vo;
    }
}
